# -*- coding: utf-8 -*-
# !/usr/resources/python3

import re
import ngender
from sqlalchemy import and_
from models.Model import Model
from utils.Utility import Utility
from models.ResourceExclude import ResourceExcludeModel as Exclude
from models.ResourceTitle import ResourceTitleModel as Title

# model
model = Model()


class PersonNameExtract:
    # 提取人名
    @staticmethod
    def extract_names(data, exists, exclude):
        # 特殊字符
        regex_special = re.compile(r"[…@_!#$%^&*()<>?/\|}{~:]")
        # 标点字符
        regex_mark = re.compile(r"[^\w\s]")
        # 收集全部姓名
        for items in data:
            for p in items:
                tag = p[1]
                text = p[0]
                if tag == 'PERSON' \
                        and len(text) > 1 \
                        and text not in exclude \
                        and regex_special.search(text) is None \
                        and regex_mark.search(text) is None:
                    if text not in exists:
                        try:
                            res = ngender.guess(text)
                            sex = res[0]
                            if sex == 'unknown':
                                sex = 'male'
                            exists[text] = [sex, round(res[1], 2), 1]
                        except AssertionError as e:
                            exists[text] = ['male', 1, 1]
                    else:
                        exists[text][2] = exists[text][2] + 1

        return exists

    # 提取人名(LAC)
    @staticmethod
    def extract_names_lac(data, exists, exclude):
        # 特殊字符
        regex_special = re.compile(r"[…@_!#$%^&*()<>?/\|}{~:]")
        # 标点字符
        regex_mark = re.compile(r"[^\w\s]")
        text_tok = []
        text_ner = []
        # 收集全部姓名
        for items in data:
            text_tok.append(items[0])
            text_ner.append(items[1])
            for idx, tag in enumerate(items[1]):
                text = items[0][idx]
                if tag == 'PER' \
                        and len(text) > 1 \
                        and text not in exclude\
                        and regex_special.search(text) is None \
                        and regex_mark.search(text) is None:
                    if text not in exists:
                        try:
                            res = ngender.guess(text)
                            sex = res[0]
                            if sex == 'unknown':
                                sex = 'male'
                            exists[text] = [sex, round(res[1], 2), 1]
                        except AssertionError as e:
                            exists[text] = ['male', 1, 1]
                    else:
                        exists[text][2] = exists[text][2] + 1

        return exists, text_tok, text_ner

    # 提取字典
    @staticmethod
    def dict_extract(persons):
        dicts = []
        for n in persons.values():
            dicts.extend(list(n.keys()))

        return dicts

    # 获取排除名
    @staticmethod
    def get_exclude():
        resources = model.all(Exclude, and_(
            Exclude.text != ''
        ))
        data = []
        for i in resources:
            data.append(i.text)

        return data

    # 职位称呼
    @staticmethod
    def get_title():
        resources = model.all(Title, and_(
            Title.text != ''
        ))
        data = []
        for i in resources:
            data.append(i.text)

        return data
